<?php
namespace app\admin\controller;
use think\Lang;
use app\common\model\Goodsclass;
/**
 * ============================================================================
 * DSMall多用户商城
 * ============================================================================
 * 版权所有 2014-2028 长沙德尚网络科技有限公司，并保留所有权利。
 * 网站地址: http://www.csdeshang.com
 * ----------------------------------------------------------------------------
 * 这不是一个自由软件！您只能在不用于商业目的的前提下对程序代码进行修改和使用 .
 * 不允许对程序代码以任何形式任何目的的再发布。
 * ============================================================================
 * 控制器
 */
class Promotionmansong extends AdminControl
{
    var $_store_id;
    public function _initialize()
    {
        parent::_initialize(); // TODO: Change the autogenerated stub
        Lang::load(APP_PATH . 'admin/lang/'.config('default_lang').'/promotionmansong.lang.php');
        $this->_store_id = session('store_id');
    }

    /**
     * 活动列表
     **/
    public function index()
    {
        //自动开启满就送
        if (intval(input('param.promotion_allow')) === 1) {
            $config_model = model('config');
            $update_array = array();
            $update_array['promotion_allow'] = 1;
            $config_model->editConfig($update_array);
        }
        $mansong_model = model('pmansong');

        $param = array();
        if (!empty(input('param.mansong_name'))) {
            $param['mansong_name'] = array('like', '%' . input('param.mansong_name') . '%');
        }
        if (!empty(input('param.store_name'))) {
            $param['store_name'] = array('like', '%' . input('param.store_name') . '%');
        }
        if (!empty(input('param.state'))) {
            $param['mansong_state'] = input('param.state');
        }
        //分类查询
        $search_gc_id1 = input('get.search_gc_id1', '');
        $search_gc_id2 = input('get.search_gc_id2', '');
        $search_gc_id3 = input('get.search_gc_id3', '');
        if (!empty($search_gc_id1)) {
            $param['gc_one_id'] = ['eq', $search_gc_id1];
        }
        if (!empty($search_gc_id2)) {
            $param['gc_two_id'] = ['eq', $search_gc_id2];
        }
        if (!empty($search_gc_id3)) {
            $param['gc_three_id'] = ['eq', $search_gc_id3];
        }
        $mansong_list = $mansong_model->getMansongList($param, 10);

        foreach ($mansong_list as $key => $value) {
            //获取分类名称
            $name = db('goodsclass')->where(['gc_id' => $value['gc_three_id']])->find();
            $name1 = db('goodsclass')->where(['gc_id' => $value['gc_two_id']])->find();
            $name2 = db('goodsclass')->where(['gc_id' => $value['gc_one_id']])->find();
            
            if(!empty($name2) && !empty($name1) && !empty($name)){
                $mansong_list[$key]['class_name'] = $name2['gc_name'] .'>'. $name1['gc_name'] .'>'. $name['gc_name'];
            }elseif (!empty($name2) && !empty($name1)) {
               $mansong_list[$key]['class_name'] = $name2['gc_name'] .'>'. $name1['gc_name'];
            }else{
                $mansong_list[$key]['class_name'] = $name2['gc_name'];
            }
        }
        
        

        $this->assign('mansong_list', $mansong_list);
        $this->assign('show_page', $mansong_model->page_info->render());
        $this->assign('mansong_state_array', $mansong_model->getMansongStateArray());


        $this->setAdminCurItem('index');
        $this->assign('filtered', $param ? 1 : 0); //是否有查询条件

        //三级分类查询
        $class = new Goodsclass();
        $parent_class_list = $class->getGoodsclassList(['gc_parent_id'=>0]);
        $this->assign('class_list',$parent_class_list);

        

        // 输出自营店铺IDS
        $this->assign('flippedOwnShopIds', array_flip(model('store')->getOwnShopIds()));
        return $this->fetch();
    }

    //添加满即送
    public function mansong_add(){
        $mansongquota_model = model('pmansongquota');
        $pmansong_model = model('pmansong');

        $start_time = $pmansong_model->getMansongNewStartTime(session('store_id'));

        $end_time=time();
        $isPlatformStore = check_platform_store() ? true: false ;
        $this->assign('isPlatformStore', $isPlatformStore);

//        if (!$isPlatformStore) {
//            //检查当前套餐是否可用
//            $current_mansong_quota = $mansongquota_model->getMansongquotaCurrent(session('store_id'));
//            if (empty($current_mansong_quota)) {
//                if(intval(config('promotion_mansong_price'))!=0){
//                    $this->error(lang('mansong_quota_current_error'));
//                }else{
                    $current_mansong_quota=array('mansongquota_starttime'=>TIMESTAMP,'mansongquota_endtime'=>TIMESTAMP+86400*30*12);//没有套餐时，最多一个月
//                }
//            }

            if (empty($start_time)) {
                $start_time = $current_mansong_quota['mansongquota_starttime'];
            }
            $end_time = $current_mansong_quota['mansongquota_endtime'];
//        }

        if (empty($start_time))
            $start_time = time();

        $this->assign('start_time', $start_time);
        $this->assign('end_time', $end_time);

        $goods_model = model('goods');
        /**
         * 处理商品分类
         */
        $id = input('param.choose_gcid');
        $choose_gcid = ($t = intval(input('param.choose_gcid'))) > 0 ? $t : 0;
        $gccache_arr = model('goodsclass')->getGoodsclassCache($choose_gcid, 3);

        $this->assign('gc_json', json_encode($gccache_arr['showclass']));
        $this->assign('gc_choose_json', json_encode($gccache_arr['choose_gcid']));

        // 获取商品分类
        $goodsclass_model = model('goodsclass');
        $gc_list = $goodsclass_model->getGoodsclass($this->_store_id);
        $this->assign('gc_list', $gc_list);

        $brand_array = [
            'mansong_state' => 1,
        ];
        $this->assign('brand_array', $brand_array);
        //输出导航
//        $this->setSellerCurMenu('Sellerpromotionmansong');
        $this->setAdminCurItem('mansong_add');
        return $this->fetch();

    }
    /**
     * 保存添加的满就送活动
     **/
    public function mansong_save()
    {
        $data = input('post.');
        $mansong_name = trim(input('post.mansong_name'));
        $start_time = strtotime(input('post.start_time'));
        $end_time = strtotime(input('post.end_time'));
        $platform_share = intval(input('post.platform_share'));
        $proportion_share = intval(input('post.proportion_share'));
        $supplier_share = intval(input('post.supplier_share'));
        $goods_class_id = intval(input('post.choose_gcid'));
        $mansong_state = intval(input('post.status'));

        $mansongquota_model = model('pmansongquota');
        $pmansong_model = model('pmansong');
        $mansongrule_model = model('pmansongrule');


        //承担比例
        if($platform_share + $proportion_share + $supplier_share != 100){
            ds_json_encode(10001,'承担比例错误');
        }
        if ($start_time >= $end_time) {
            ds_json_encode(10001,lang('greater_than_start_time'));
        }
        if (empty($mansong_name)) {
            ds_json_encode(10001,lang('mansong_name_error'));
        }
        if(empty($goods_class_id)){
            ds_json_encode(10001,'请选择分类');
        }

        //判断分类
        $mansong_class = db('pmansong')->find();
        $class = db('goodsclass')->where(['gc_id'=>$mansong_class['gc_id']])->find();
        if(empty($class['gc_parent_id'])){
            //如果库里存在的是一级分类  
            if($class['gc_id'] == $goods_class_id){
                ds_json_encode(10001,'同一类商品不能同时存在');
            }
        }else{
            //有父值的话再往上找
            $class_one = db('goodsclass')->where(['gc_id'=>$class['gc_parent_id']])->find();
            if(empty($class_one['gc_parent_id'])){
               //二级分类
                if($class['gc_id'] == $goods_class_id){
                    ds_json_encode(10001,'同一类商品不能同时存在');
                }
            }else{
                //三级分类
               if($class['gc_id'] == $goods_class_id){
                    ds_json_encode(10001,'同一类商品不能同时存在');
               }
               //如果是库里存的是三级分类的话，它上级都不可以被选择
            }
         }
//        $start_time_limit = $pmansong_model->getMansongNewStartTime(session('store_id'));
//        if (!empty($start_time_limit) && $start_time_limit > $start_time) {
//            $start_time = $start_time_limit;
//        }

//        if (!check_platform_store()) {
//            //检查当前套餐是否可用
//            $current_mansong_quota = $mansongquota_model->getMansongquotaCurrent(session('store_id'));
//            if (empty($current_mansong_quota)) {
//                if(intval(config('promotion_mansong_price'))!=0){
//                    ds_json_encode(10001,lang('mansong_quota_current_error'));
//                }else{
                    // $current_mansong_quota=array('mansongquota_starttime'=>TIMESTAMP,'mansongquota_endtime'=>TIMESTAMP+86400*30);//没有套餐时，最多一个月
//                }
//            }

            //验证输入
            // $quota_start_time = intval($current_mansong_quota['mansongquota_starttime']);
            // $quota_end_time = intval($current_mansong_quota['mansongquota_endtime']);

            // if ($start_time < $quota_start_time) {
            //     ds_json_encode(10001,sprintf(lang('mansong_add_start_time_explain'), date('Y-m-d', $current_mansong_quota['mansongquota_starttime'])));
            // }
            // if ($end_time > $quota_end_time) {
            //     ds_json_encode(10001,sprintf(lang('mansong_add_end_time_explain'), date('Y-m-d', $current_mansong_quota['mansongquota_endtime'])));
            // }
//        }

        if (empty(input('post.mansong_rule/a'))) {
            ds_json_encode(10001,lang('mansong_rule_empty'));
        }
        $data = input('post.mansong_rule/a');
        $param = array();
        $param['mansong_name'] = $mansong_name;
        $param['mansong_starttime'] = $start_time;
        $param['mansong_endtime'] = $end_time;
//        $param['store_id'] = session('store_id');
//        $param['store_name'] = session('store_name');
//        $param['member_id'] = session('member_id');
//        $param['member_name'] = session('member_name');
        $param['mansongquota_id'] = isset($current_mansong_quota['mansongquota_id']) ? $current_mansong_quota['mansongquota_id'] : 0;
        $param['mansong_remark'] = trim(input('post.remark'));
        $param['platform_share'] = $platform_share;
        $param['proportion_share'] = $proportion_share;
        $param['supplier_share'] = $supplier_share;
        $param['goods_class_id'] = $goods_class_id;

        //判断时间
        $mandong_list = db('pmansong')->find();
        if($mandong_list['mansong_endtime'] < $start_time){
            $param['mansong_state'] = 1;
        }else{
            $param['mansong_state'] = $mansong_state;
        }

        //处理分类
         $class = db('goodsclass')->where(['gc_id'=>$goods_class_id])->find();
         if(empty($class['gc_parent_id'])){
            //没父值的话就是一级分类id
            $param['gc_one_id'] = $class['gc_id'];
            $param['gc_id'] = $class['gc_id'];
         }else{
            //有父值的话再往上找
            $class_one = db('goodsclass')->where(['gc_id'=>$class['gc_parent_id']])->find();
            if(empty($class_one['gc_parent_id'])){ 
                $param['gc_one_id'] = $class_one['gc_id'];
                $param['gc_two_id'] = $class['gc_id'];
                $param['gc_id'] = $class['gc_id'];
            }else{
                $class_two = db('goodsclass')->where(['gc_id'=>$class_one['gc_parent_id']])->find();
                $param['gc_one_id'] =  $class_two['gc_id'];
                $param['gc_two_id'] =  $class_one['gc_id'];
                $param['gc_three_id'] = $class['gc_id'];
                $param['gc_id'] = $class['gc_id'];
            }
         }

        $mansong_id = $pmansong_model->addMansong($param);
        if ($mansong_id) {
            $mansong_rule_array = array();
            foreach (input('post.mansong_rule/a') as $k =>$value) {
                list($price, $discount, $goods_id,$new_price,$goods_num) = explode(',', $value);
               
                //获取商品名
                $goods_info = db('goods')->where(['goods_id'=>$goods_id])->find();

                $mansong_rule = array();
                $mansong_rule['mansong_id'] = $mansong_id;
                $mansong_rule['mansongrule_price'] = $price;
                $mansong_rule['mansongrule_discount'] = $discount;
                $mansong_rule['goods_id'] = $goods_id;
                $mansong_rule['new_price'] = $new_price;
                $mansong_rule['goods_num'] = $goods_num;
                $mansong_rule['mansong_goods_name'] = empty($goods_info) ? '' : $goods_info['goods_name'];
                $mansong_rule_array[] = $mansong_rule;
            }
            
            //生成规则
            $result = $mansongrule_model->addMansongruleArray($mansong_rule_array);

//            $this->recordSellerlog('添加满即送活动，活动名称：' . $mansong_name);

            // 自动发布动态
            // mansong_name,start_time,end_time,store_id
            $data_array = array();
            $data_array['mansong_name'] = $param['mansong_name'];
            $data_array['mansong_starttime'] = $param['mansong_starttime'];
            $data_array['mansong_endtime'] = $param['mansong_endtime'];
            $data_array['store_id'] = session('store_id');
            $this->storeAutoShare($data_array, 'mansong');

            ds_json_encode(10000,lang('mansong_add_success'));
        }
        else {
            ds_json_encode(10001,lang('mansong_add_fail'));
        }
    }
    /**
     * 编辑限时折扣活动
     **/
    public function mansong_edit()
    {
        $mansongquota_model = model('pmansongquota');
        $pmansong_model = model('pmansong');

        $start_time = $pmansong_model->getMansongNewStartTime(session('store_id'));

        $end_time=time();
        $isPlatformStore = check_platform_store() ? true: false ;
        $this->assign('isPlatformStore', $isPlatformStore);


        $current_mansong_quota=array('mansongquota_starttime'=>TIMESTAMP,'mansongquota_endtime'=>TIMESTAMP+86400*30);//没有套餐时，最多一个月


        if (empty($start_time)) {
            $start_time = $current_mansong_quota['mansongquota_starttime'];
        }
        $end_time = $current_mansong_quota['mansongquota_endtime'];
        if (empty($start_time))
            $start_time = time();
        $this->assign('start_time', $start_time);
        $this->assign('end_time', $end_time);
        $goods_model = model('goods');
        /**
         * 处理商品分类
         */
        $id = input('param.choose_gcid');
        $choose_gcid = ($t = intval(input('param.choose_gcid'))) > 0 ? $t : 0;
        $gccache_arr = model('goodsclass')->getGoodsclassCache($choose_gcid, 3);

        $this->assign('gc_json', json_encode($gccache_arr['showclass']));
        $this->assign('gc_choose_json', json_encode($gccache_arr['choose_gcid']));

        //输出导航
//        $this->setSellerCurMenu('Sellerpromotionmansong');

        if (check_platform_store()) {
            $this->assign('isPlatformStore', true);
        }else {
            $this->assign('isPlatformStore', false);
        }
        $where = [
            'mansong_id' => input('param.mansong_id')
        ];
        $mansong_info = $pmansong_model->getMansongInfo($where);
        $this->assign('mansong_info', $mansong_info);

        $brand_array = [
            'mansong_state' => $mansong_info['mansong_state'],
        ];
        $this->assign('brand_array', $brand_array);

        //满即送规则
        $mansong_id = intval(input('param.mansong_id'));
        $mansong_model = model('pmansong');
        $mansongrule_model = model('pmansongrule');
        $param = array();
        $param['mansong_id'] = $mansong_id;
        $mansongrule_list = $mansongrule_model->getMansongruleListByID($mansong_id);

        $this->assign('mansongrule_list', $mansongrule_list);

        $count  = db('pmansongrule')->where(['mansong_id'=>$mansong_id])->count();
        $this->assign('count', $count);

        //获取分类信息
        $class_info = db('goodsclass')->where(['gc_parent_id' => 0])->select();
        $this->assign('class_info', $class_info);

        // 获取商品分类
        $goodsclass_model = model('goodsclass');
        $gc_list = $goodsclass_model->getGoodsclass($this->_store_id);
        $this->assign('gc_list', $gc_list);

        //获取分类名称
        $name = db('goodsclass')->where(['gc_id' => $mansong_info['gc_id']])->find();
        $name1 = db('goodsclass')->where(['gc_id' => $name['gc_parent_id']])->find();
        $name2 = db('goodsclass')->where(['gc_id' => $name1['gc_parent_id']])->find();


        if(empty($name1)){
            $class_name = $name['gc_name'];
        }elseif (empty($name2)) {
            $class_name = $name1['gc_name'] .'>'. $name['gc_name'];
        }elseif(!empty($name) && !empty($name1) && !empty($name2)){
            $class_name = $name2['gc_name'] .'>'. $name1['gc_name'] .'>'. $name['gc_name'];
        }
        $this->assign('class_name', $class_name);

        $this->setAdminCurItem('mansong_add');
        return $this->fetch('mansong_add');
    }
    //删除
    public  function del_rule(){
        $rule_id = intval(input('param.rule_id'));
        $result1 = db('pmansongrule')->where(['mansongrule_id'=>$rule_id])->delete();
        if ($result1) {
            ds_json_encode(10000, lang('ds_common_del_succ'));
        } else {
            ds_json_encode(10001, lang('ds_common_del_fail'));
        }
    }
    //编辑保存
    public function mansong_edit_save()
    {
        $mansong_id = trim(input('post.mansong_id'));
        $mansong_name = trim(input('post.mansong_name'));
        $start_time = strtotime(input('post.start_time'));
        $end_time = strtotime(input('post.end_time'));
        $platform_share = intval(input('post.platform_share'));
        $proportion_share = intval(input('post.proportion_share'));
        $supplier_share = intval(input('post.supplier_share'));
        $goods_class_id = intval(input('post.choose_gcid'));
        $mansong_state = intval(input('post.status'));

        $deep = intval(input('post.deep'));



        $mansongquota_model = model('pmansongquota');
        $pmansong_model = model('pmansong');
        $mansongrule_model = model('pmansongrule');


        //承担比例
        if($platform_share + $proportion_share + $supplier_share != 100){
            ds_json_encode(10001,'承担比例错误');
        }
//        if ($start_time >= $end_time) {
//            ds_json_encode(10001,lang('greater_than_start_time'));
//        }
        if (empty($mansong_name)) {
            ds_json_encode(10001,lang('mansong_name_error'));
        }

        $current_mansong_quota=array('mansongquota_starttime'=>TIMESTAMP,'mansongquota_endtime'=>TIMESTAMP+86400*30);//没有套餐时，最多一个月

        //验证输入
        $quota_start_time = intval($current_mansong_quota['mansongquota_starttime']);
        $quota_end_time = intval($current_mansong_quota['mansongquota_endtime']);

//        if ($start_time < $quota_start_time) {
//            ds_json_encode(10001,sprintf(lang('mansong_add_start_time_explain'), date('Y-m-d', $current_mansong_quota['mansongquota_starttime'])));
//        }
//        if ($end_time > $quota_end_time) {
//            ds_json_encode(10001,sprintf(lang('mansong_add_end_time_explain'), date('Y-m-d', $current_mansong_quota['mansongquota_endtime'])));
//        }
//        }

        if (empty(input('post.mansong_rule/a'))) {
            //ds_json_encode(10001,lang('mansong_rule_empty'));
        }
        $data = input('post.mansong_rule/a');


        $param = array();
        $param['mansong_name'] = $mansong_name;
        $param['mansong_starttime'] = $start_time;
        $param['mansong_endtime'] = $end_time;
        $param['mansongquota_id'] = isset($current_mansong_quota['mansongquota_id']) ? $current_mansong_quota['mansongquota_id'] : 0;
        $param['mansong_remark'] = trim(input('post.remark'));
        $param['platform_share'] = $platform_share;
        $param['proportion_share'] = $proportion_share;
        $param['supplier_share'] = $supplier_share;
        $param['goods_class_id'] = $goods_class_id;
        $param['mansong_state'] = $mansong_state;
 
        //处理分类
        $class = db('goodsclass')->where(['gc_id'=>$goods_class_id])->find();
        if($deep == 1){
            $param['gc_id'] = $class['gc_id'];
            $param['gc_one_id'] = $class['gc_id'];
            $param['gc_two_id'] =  '';
            $param['gc_three_id'] = '';
        }elseif ($deep == 2) {
            $class_one = db('goodsclass')->where(['gc_id'=>$class['gc_parent_id']])->find();
            $param['gc_one_id'] = $class_one['gc_id'];
            $param['gc_two_id'] = $class['gc_id'];
            $param['gc_id'] = $class['gc_id'];
            $param['gc_three_id'] = '';
        }else{

            //处理分类
            $class = db('goodsclass')->where(['gc_id'=>$goods_class_id])->find();
            // if(empty($class['gc_parent_id'])){
            //     //没父值的话就是一级分类id
            //     $param['gc_one_id'] = $class['gc_id'];
            //     $param['gc_id'] = $class['gc_id'];
            // }else{
                //有父值的话再往上找
                $class_one = db('goodsclass')->where(['gc_id'=>$class['gc_parent_id']])->find();
                // if(empty($class_one['gc_parent_id'])){ 
                //     $param['gc_one_id'] = $class_one['gc_id'];
                //     $param['gc_two_id'] = $class['gc_id'];
                //     $param['gc_id'] = $class['gc_id'];
                // }else{
                    $class_two = db('goodsclass')->where(['gc_id'=>$class_one['gc_parent_id']])->find();
                    $param['gc_one_id'] =  $class_two['gc_id'];
                    $param['gc_two_id'] =  $class_one['gc_id'];
                    $param['gc_three_id'] = $class['gc_id'];
                    $param['gc_id'] = $class['gc_id'];
                // }
            // }

        }



        $res = $pmansong_model->editMansong($param, array('mansong_id' => $mansong_id));
        if ($mansong_id) {

            if(!empty($data)){
                $mansong_rule_array = array();
                foreach (input('post.mansong_rule/a') as $value) {
                    list($price, $discount, $goods_id,$new_price,$goods_num) = explode(',', $value);

                    //获取商品名
                    $goods_info = db('goods')->where(['goods_id'=>$goods_id])->find();
                    
                    $mansong_rule = array();
                    $mansong_rule['mansong_id'] = $mansong_id;
                    $mansong_rule['mansongrule_price'] = $price;
                    $mansong_rule['mansongrule_discount'] = $discount;
                    $mansong_rule['goods_id'] = $goods_id;
                    $mansong_rule['new_price'] = $new_price;
                    $mansong_rule['goods_num'] = $goods_num;
                    $mansong_rule['mansong_goods_name'] = empty($goods_info) ? '' : $goods_info['goods_name'];
                    $mansong_rule_array[] = $mansong_rule;
                }
                //生成规则
                $result = $mansongrule_model->addMansongruleArray($mansong_rule_array);
            }

            // 自动发布动态
            // mansong_name,start_time,end_time,store_id
            $data_array = array();
            $data_array['mansong_name'] = $param['mansong_name'];
            $data_array['mansong_starttime'] = $param['mansong_starttime'];
            $data_array['mansong_endtime'] = $param['mansong_endtime'];
            $data_array['store_id'] = session('store_id');
            $this->storeAutoShare($data_array, 'mansong');
            ds_json_encode(10000,'编辑成功');
        }
        else {
            ds_json_encode(10001,'编辑失败');
        }
    }
    /*
     * 上下架
     */

    public function status()
    {
        $id = trim(input('param.id'));
        $status = trim(input('param.status'));

        if (empty($id)) {
            ds_json_encode(10001, '操作失败');
        }
        $info = db('pmansong')->where('mansong_id', $id)->find();
        if ($status == $info['mansong_state']) {
            ds_json_encode(10001, '操作失败');
        }
        $data = array();
        $data['mansong_state'] = $status;
        $rs = db('pmansong')->where('mansong_id', $id)->data($data)->update();
        if ($rs) {
//            $this->log(lang('ds_del') . lang('admin_voucher_priceadd') . '[ID:' . $id . ']');
            ds_json_encode(10000, '操作成功');
        } else {
            ds_json_encode(10001, '操作失败');
        }
    }
    /**
     * 活动详细信息
     * temp
     **/
    public function mansong_detail()
    {
        $mansong_id = intval(input('param.mansong_id'));

        $mansong_model = model('pmansong');
        $mansongrule_model = model('pmansongrule');

        $mansong_info = $mansong_model->getMansongInfoByID($mansong_id);
        if (empty($mansong_info)) {
            $this->error(lang('param_error'));
        }
        $this->assign('xianshi_info', $mansong_info);
        $this->assign('mansong_info', $mansong_info);

        //获取分类名称
        $name = db('goodsclass')->where(['gc_id' => $mansong_info['gc_id']])->find();
        $name1 = db('goodsclass')->where(['gc_id' => $name['gc_parent_id']])->find();
        $name2 = db('goodsclass')->where(['gc_id' => $name1['gc_parent_id']])->find();
        if(empty($name1)){
            $class_name = $name['gc_name'];
        }elseif (empty($name2)) {
            $class_name = $name1['gc_name'] .'>'. $name['gc_name'];
        }elseif(!empty($name) && !empty($name1) && !empty($name2)){
            $class_name = $name2['gc_name'] .'>'. $name1['gc_name'] .'>'. $name['gc_name'];
        }
        



        $this->assign('class_name', $class_name);


        $param = array();
        $param['mansong_id'] = $mansong_id;
        $mansongrule_list = $mansongrule_model->getMansongruleListByID($mansong_id);

        $this->assign('mansongrule_list', $mansongrule_list);
        $this->setAdminCurItem('mansong_detail');

        return $this->fetch();
    }

    /**
     * 活动列表
     * temp
     **/
    public function mansong_stration()
    {
        $mansong_id = intval(input('param.mansong_id'));

        $mansong_model = model('pmansong');
        $mansongrule_model = model('pmansongrule');

        $mansong_info = $mansong_model->getMansongInfoByID($mansong_id);
        if (empty($mansong_info)) {
            $this->error(lang('param_error'));
        }
        $this->assign('xianshi_info', $mansong_info);
        $this->assign('mansong_info', $mansong_info);

        $param = array();
        $param['mansong_id'] = $mansong_id;
        $mansongrule_list = $mansongrule_model->getMansongruleListByID($mansong_id);

        $this->assign('mansongrule_list', $mansongrule_list);
        $this->setAdminCurItem('mansong_detail');

        return $this->fetch();
    }
    /**
     * 满即送活动取消
     **/
    public function mansong_cancel()
    {
        $mansong_id = intval(input('param.mansong_id'));
        
        if ($mansong_id<=0) {
            ds_json_encode(10001, lang('param_error'));
        }
        
        $mansong_model = model('pmansong');
        $result = $mansong_model->cancelMansong(array('mansong_id' => $mansong_id));
        if ($result) {
            $this->log('取消满即送活动，活动编号' . $mansong_id);
            ds_json_encode(10000, lang('ds_common_del_succ'));
        }
        else {
            ds_json_encode(10001, lang('ds_common_del_fail'));
        }
    }

    /**
     * 满即送活动删除
     **/
    public function mansong_del() {
        $mansong_model = model('pmansong');
        $mansong_id = input('param.mansong_id');
        $mansong_id_array = ds_delete_param($mansong_id);
        if($mansong_id_array === FALSE){
            ds_json_encode(10001, lang('param_error'));
        }
        $condition = array('mansong_id' => array('in', $mansong_id_array));
        $result =$mansong_model->delMansong($condition);
        if ($result) {
            $this->log('删除满即送活动，活动编号' . implode(',', $mansong_id_array));
            ds_json_encode(10000, lang('ds_common_del_succ'));
        } else {
            ds_json_encode(10001, lang('ds_common_del_fail'));
        }
    }

    /**
     * 选择活动商品
     **/
    public function search_goods()
    {
        $goods_model = model('goods');
        $condition = array();
//        $condition['store_id'] = session('store_id');
//        $condition['goods_name'] = array('like', '%' . input('param.goods_name') . '%');
//        $goods_list = $goods_model->getGeneralGoodsList($condition, '*', 8);
        $condition['goods.goods_name'] = array('like', '%' . input('param.goods_name') . '%');
        $goods_list = $goods_model->getGoodsListForPromotion($condition, 'goods.goods_id,goods.goods_commonid,goods.goods_name,goods.goods_image,goods.goods_price', 10, 'bundling');
        foreach($goods_list as $k => $v){
           $goods_list[$k]['goods_num'] = 1;
        }
        $this->assign('goods_list', $goods_list);
        $this->assign('show_page', $goods_model->page_info->render());
        echo $this->fetch('mansong_goods');
    }
    

    /**
     * 套餐管理
     **/
    public function mansong_quota()
    {
        $mansongquota_model = model('pmansongquota');

        $param = array();
        if (!empty(input('param.store_name'))) {
            $param['store_name'] = array('like', '%' . input('param.store_name') . '%');
        }
        $mansongquota_list = $mansongquota_model->getMansongquotaList($param, 10, 'mansongquota_id desc');
        $this->assign('mansongquota_list', $mansongquota_list);
        $this->assign('show_page', $mansongquota_model->page_info->render());
        $this->setAdminCurItem('mansong_quota');

        return $this->fetch();

    }
    /**
     * 自动发布店铺动态
     *
     * @param array $data 相关数据
     * @param string $type 类型 'new','coupon','xianshi','mansong','bundling','groupbuy'
     *            所需字段
     *            new       goods表'             goods_id,store_id,goods_name,goods_image,goods_price,goods_transfee_charge,goods_freight
     *            xianshi   pxianshigoods表'   goods_id,store_id,goods_name,goods_image,goods_price,goods_freight,xianshi_price
     *            mansong   pmansong表'         mansong_name,start_time,end_time,store_id
     *            bundling  pbundling表'        bl_id,bl_name,bl_img,bl_discount_price,bl_freight_choose,bl_freight,store_id
     *            groupbuy  goodsgroup表'       group_id,group_name,goods_id,goods_price,groupbuy_price,group_pic,rebate,start_time,end_time
     *            coupon在后台发布
     */
    public function storeAutoShare($data, $type)
    {
        $param = array(
            3 => 'new',
            4 => 'coupon',
            5 => 'xianshi',
            6 => 'mansong',
            7 => 'bundling',
            8 => 'groupbuy'
        );
        $param_flip = array_flip($param);
        if (!in_array($type, $param) || empty($data)) {
            return false;
        }

        $auto_setting = model('storesnssetting')->getStoresnssettingInfo(array('storesnsset_storeid' => session('store_id')));
        $auto_sign = false; // 自动发布开启标志

        if ($auto_setting['storesnsset_' . $type] == 1) {
            $auto_sign = true;

            $goodsdata = addslashes(json_encode($data));
            if ($auto_setting['storesnsset_' . $type . 'title'] != '') {
                $title = $auto_setting['storesnsset_' . $type . 'title'];
            } else {
                $auto_title = 'ds_store_auto_share_' . $type . rand(1, 5);
                $title = lang($auto_title);
            }
        }
        if ($auto_sign) {
            // 插入数据
            $stracelog_array = array();
            $stracelog_array['stracelog_storeid'] = $this->store_info['store_id'];
            $stracelog_array['stracelog_storename'] = $this->store_info['store_name'];
            $stracelog_array['stracelog_storelogo'] = empty($this->store_info['store_avatar']) ? '' : $this->store_info['store_avatar'];
            $stracelog_array['stracelog_title'] = $title;
            $stracelog_array['stracelog_content'] = '';
            $stracelog_array['stracelog_time'] = TIMESTAMP;
            $stracelog_array['stracelog_type'] = $param_flip[$type];
            $stracelog_array['stracelog_goodsdata'] = $goodsdata;
            model('storesnstracelog')->addStoresnstracelog($stracelog_array);
            return true;
        } else {
            return false;
        }
    }
    /**
     * ajax删除规格值
     */
    public function ajax_delspec()
    {
        $spvalue_id = intval(input('param.id'));
        if ($spvalue_id <= 0) {
            echo 'false';
            exit();
        }
        $rs = model('spec')->delSpecvalue(array('spvalue_id' => $spvalue_id, 'store_id' => $this->_store_id));
        if ($rs) {
            echo 'true';
            exit();
        }
        else {
            echo 'false';
            exit();
        }
    }

    /**
     * AJAX获取商品分类
     */
    public function ajax_class()
    {
        $id = intval(input('param.id'));
        $deep = intval(input('param.deep'));
        if ($id <= 0 || $deep <= 0 || $deep >= 4) {
            echo 'false';
            exit();
        }
        $deep += 1;
        $goodsclass_model = model('goodsclass');

        // 验证分类是否存在
        $gc_info = $goodsclass_model->getGoodsclassInfoByIds($id);
        if (empty($gc_info)) {
            echo 'false';
            exit();
        }

        // 读取商品分类
        if ($deep != 4) {
            $gc_list = $goodsclass_model->getGoodsclass($this->_store_id, $id, $deep);
        }
        // 分类不为空输出分类信息
        if (!empty($gc_list)) {
            $data = array('type' => 'class', 'data' => $gc_list, 'deep' => $deep);
        }
        else {
            // 查询类型
            $type_model = model('type');
            $spec_list = $type_model->getSpecByType(array('type_id' => $gc_info['type_id']), 't.type_id, s.*');

            $data = array('type' => 'spec', 'data' => $spec_list, 'gcid' => $id, 'aa' =>$gc_info, 'gcid' => $id, 'deep' => $deep);
        }

        echo json_encode($data);
        exit();
    }
    /**
     * 设置
     **/
    public function mansong_setting()
    {
        if (!(request()->isPost())) {
            $setting = rkcache('config', true);
            $this->assign('setting', $setting);
            $this->setAdminCurItem('mansong_setting');
            return $this->fetch();
        } else {
            $promotion_mansong_price = intval(input('post.promotion_mansong_price'));
            if ($promotion_mansong_price < 0) {
                $this->error(lang('param_error'));
            }

            $config_model = model('config');
            $update_array = array();
            $update_array['promotion_mansong_price'] = $promotion_mansong_price;

            $result = $config_model->editConfig($update_array);
            if ($result === true) {
                $this->log(lang('ds_config') . lang('ds_promotion_mansong') . lang('mansong_price'));
                dsLayerOpenSuccess(lang('setting_save_success'));
            } else {
                $this->error(lang('setting_save_fail'));
            }
        }
    }

    /**
     * 页面内导航菜单
     *
     * @param string $menu_key 当前导航的menu_key
     * @param array $array 附加菜单
     * @return
     */
    protected function getAdminItemList()
    {
        $menu_array = array(
            array(
                'name' => 'index', 
                'text' => lang('mansong_list'), 
                'url' => url('Promotionmansong/index')
            ), 
            //array(
                //'name' => 'mansong_quota',
               // 'text' => lang('mansong_quota'),
                //'url' => url('Promotionmansong/mansong_quota')
            //), array(
               // 'name' => 'mansong_setting',
               // 'text' => lang('mansong_setting'),
                //'url' => "javascript:dsLayerOpen('".url('Promotionmansong/mansong_setting')."','".lang('mansong_setting')."')"
           // ),
            array(
                'name' => 'mansong_add',
                'text' => '添加满即送',
                'url' => url('Promotionmansong/mansong_add')
            ),
        );
        if (request()->action() == 'mansong_detail') {
            $menu_array[] = array(
                'name' => 'mansong_detail', 'text' => lang('mansong_detail'),
                'url' => url('Promotionmansong/mansong_detail')
            );
        }
        if (request()->action() == 'mansong_edit') {
            $menu_array[] = array(
                'name' => 'mansong_add',
                'text' => '编辑满即送',
                'url' => url('Promotionmansong/mansong_add')
            );
            unset($menu_array[1]);
        }
        return $menu_array;
    }
}